
var XML_KWS= new CT_Module("XML_KWS","keywords.xml","keywords.xml","keywords");


function kw_find(txt,lang) {
  return XML_KWS.getRoot().selectSingleNode("/keywords/keyword[@name!=\"skipkeyword\"]/"+lang+"/kw[text() = \""+txt+"\"]");
}

function kw_findsubstr(txt,lang) {
  return XML_KWS.getRoot().selectSingleNode("/keywords/keyword[@name!=\"skipkeyword\"]/"+lang+"/kw[ contains( \""+txt+"\", text()) ]");
}

function kw_isskip(txt,lang) {
  return XML_KWS.getRoot().selectSingleNode("/keywords/keyword[@name=\"skipkeyword\"]/"+lang+"/kw[text() = \""+txt+"\"]");
}

function kw_node(n) {
  if(n) {
    this.node= n.parentNode.parentNode;
    this.getCat = function() { return this.node.getAttribute("category"); }
    this.getName = function() { return this.node.getAttribute("name"); }
    this.isUsrCat = function() { return this.node.getAttribute("usrcat")=="yes"; }
   } else {
    this.getCat = function() { return "";}
    this.getName = function() { return "";}
    this.isUsrCat = function() { return false;}
  }
}

function kwkws_iterator(node,lang){
  this.base=array_iterator;
  this.base(node.selectSingleNode(lang.toLowerCase()).childNodes);
}
kwkws_iterator.prototype= new array_iterator;
kwkws_iterator.prototype.constructor=kwkws_iterator;
kwkws_iterator.prototype.getValue=function() { return this.getElem().text;}


function kw_iterator(query) {
  this.base=array_iterator;
  this.base(XML_KWS.getRoot().selectNodes("/keywords/keyword["+query+"]"));
}

kw_iterator.prototype= new array_iterator;
kw_iterator.prototype.constructor=kw_iterator;


kw_iterator.prototype.getCat=function() { return this.getElem().getAttribute("category");}
kw_iterator.prototype.getCatName=function(lang) { 
  return this.getElem().selectSingleNode(lang.toLowerCase()).getAttribute("name");
}
kw_iterator.prototype.getColor1=function() { return this.getElem().getAttribute("color_i");}
kw_iterator.prototype.getColor2=function() { return this.getElem().getAttribute("color_d");}
kw_iterator.prototype.isActive=function() { return this.getElem().getAttribute("active")=="yes";}
kw_iterator.prototype.isCatMaxOne=function() { return this.getElem().getAttribute("maxone")=="yes";}
kw_iterator.prototype.keywords=function(lang) { return  new kwkws_iterator(this.getElem(),lang);}

function kwname_iterator(name,lang) {
  this.base=array_iterator;
  this.base(XML_KWS.getRoot().selectSingleNode("/keywords/keyword[@name=\""+name+"\"]/"+lang.toLowerCase()).childNodes);
}
kwname_iterator.prototype= new array_iterator;
kwname_iterator.prototype.constructor=kwname_iterator;
kwname_iterator.prototype.getValue=function() { return this.getElem().text;}


  
function kw_getCatName(cat,lang) {
  var node=XML_KWS.getRoot().selectSingleNode("/keywords/keyword[@category=\""+cat+"\"]/"+lang.toLowerCase());
  return (node && node.getAttribute("name")) || "unknown";
}

function kw_getColor1(cat) {
  var node=XML_KWS.getRoot().selectSingleNode("/keywords/keyword[@category=\""+cat+"\"]"); 
  return  (node && node.getAttribute("color_i"));
}

function kw_getColor2(cat) {
  var node=XML_KWS.getRoot().selectSingleNode("/keywords/keyword[@category=\""+cat+"\"]"); 
  return  (node && node.getAttribute("color_d"));
}

function kw_SwitchActiveCat(cat) {
 var node=XML_KWS.getRoot().selectSingleNode("/keywords/keyword[@category=\""+cat+"\"]"); 
 if(node.getAttribute("active")) {
   node.setAttribute("active","");
 } else {
   node.setAttribute("active","yes");
 }
 XML_KWS.save();
}

function kw_SwitchCatMaxOne(cat) {
 var node=XML_KWS.getRoot().selectSingleNode("/keywords/keyword[@category=\""+cat+"\"]"); 
 if(node.getAttribute("maxone")) {
   node.setAttribute("maxone","");
 } else {
   node.setAttribute("maxone","yes");
 }
 XML_KWS.save();
}

function kw_isCatMaxOne(cat) {
 var node=XML_KWS.getRoot().selectSingleNode("/keywords/keyword[@category=\""+cat+"\"]"); 
 return node && node.getAttribute("maxone")=="yes"; 
}


XML_KWS.LoadCategories=function(obj) {
  XML_KWS.CLSTYPE=[];
  XML_KWS.CLSTYPENAME=[];

  var kw_iter=new kw_iterator("@active=\"yes\"");

  while(!kw_iter.end()) {
    XML_KWS.CLSTYPE.push(kw_iter.getCat());
    XML_KWS.CLSTYPENAME.push(kw_iter.getCatName("en"));
    kw_iter.next();
  }
  XML_KWS.CLSTYPE.push("O");
  XML_KWS.CLSTYPENAME.push("Other");

}

function cltype_iterator() {
   this._idx=0;
}
cltype_iterator.prototype.getValue = function () { return XML_KWS.CLSTYPE[this._idx];}
cltype_iterator.prototype.next     = function () { this._idx++;}
cltype_iterator.prototype.end      = function () { return this._idx >= XML_KWS.CLSTYPENAME.length; }
cltype_iterator.prototype.getName  = function () { return XML_KWS.CLSTYPENAME[this._idx];}
cltype_iterator.prototype.getType  = function () { return XML_KWS.CLSTYPE[this._idx];} 


if(!XML_KWS.getDOM()) {
  alert("Error creating object XMLKWS");
} else {
  XML_KWS.getDOM().setProperty("SelectionLanguage", "XPath"); 
  XML_KWS.getDOM().async=false;
  XML_KWS.LoadCategories();
}
